package zero.tech.games.gate.topic;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import zero.tech.core.ZeroContext;
import zero.tech.core.data.Resp;
import zero.tech.games.common.ClubMemberUtil;
import zero.tech.games.common.Code;
import zero.tech.games.common.TopicOrder;
import zero.tech.games.common.marquee.Marquee;
import zero.tech.games.common.marquee.MarqueeContent;
import zero.tech.games.entity.games.ClubMember;
import zero.tech.games.gate.data.GATECMD;
import zero.tech.redis.topic.TopicChannelType;
import zero.tech.redis.topic.TopicProcess;
import zero.tech.wsnet.controller.CacheOnline;
import zero.tech.wsnet.controller.Controller;
import zero.tech.wsnet.data.Message;

public class MarqueeTopic extends TopicProcess{

	@Override
	public String orderType() {
		return TopicOrder.MARQUEE;
	}

	@Override
	public void process(String message) throws Exception {
		Marquee marquee = JSONObject.parseObject(message, Marquee.class);
		Map<Integer,Controller> onlines=CacheOnline.getCache().getOnlines();
		ClubMemberUtil clubMemberUtil = ZeroContext.getInstance(ClubMemberUtil.class);
		List<Integer> uids=marquee.getUids();
		MarqueeContent marqueeContent =new MarqueeContent(marquee.getMsg());
		if(marquee.getClub_id()==null) {
			for(Integer uid:uids) {
				Controller controller=onlines.get(uid);
				if(controller!=null&&controller.getWebClient()!=null&&controller.getWebClient().isAlive()) {
					controller.getWebClient().sendMessage(Message.build(GATECMD.NOTIFY_MARQUEE, Resp.build(Code.OK, "跑马灯到达",marqueeContent)));
				}
			}
		}else {
			//指定俱乐部玩家发送
			int club_id=marquee.getClub_id();
			int[] uidsarr=new int[uids.size()];
			int index=0;
			for(int uid:uids) {
				uidsarr[index++]=uid;
			}
			Map<Integer,ClubMember> map = clubMemberUtil.getClubMemberBatch(club_id, uidsarr);
			for(ClubMember member:map.values()) {
				if(member!=null) {
					int uid=member.getUid();
					Controller controller = onlines.get(uid);
					if(controller!=null&&controller.getWebClient()!=null&&controller.getWebClient().isAlive()) {
						controller.getWebClient().sendMessage(Message.build(GATECMD.NOTIFY_MARQUEE, Resp.build(Code.OK, "跑马灯到达",marqueeContent)));
					}
				}
			}
		}
	}

	@Override
	public TopicChannelType channelType() {
		return TopicChannelType.MARQUEE;
	}

}
